package com.hdax.dm.user.controller;

import com.hdax.dm.common.exception.DmException;
import com.hdax.dm.user.service.UserLoginService;
import com.hdax.dm.user.service.UserenrollService;
import com.hdax.dm.utils.CommonResponse;
import com.hdax.dm.utils.ResponseUtil;
import com.hdax.dm.utils.Token;
import com.mysql.cj.callback.UsernameCallback;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping(path = "/user")
public class UserLoginController {

    @Autowired
    private UserLoginService userLoginService;
    @Autowired
    private UserenrollService userenrollService;
    @Autowired
    private Token tokenUtil;
    /**
     * 获取验证码
     * @param params
     * @return
     * @throws DmException
     */
    @PostMapping(path = "index/msg")
    public CommonResponse msg(@RequestBody Map<String, String> params) throws DmException {
        return userLoginService.msg(params);
    }

    /**
     * 用户短信登录接口
     * @return
     */
    @RequestMapping("index/smslogin")
    public CommonResponse msgLogin(@RequestBody  Map<String,String> params)throws DmException{
        return userLoginService.Smslogin(params);
    }

    /**
     * 手机号,密码登录
     * @return
     */
    @PostMapping(path = "index/login")
    public CommonResponse Login(@RequestBody Map<String,String> params)throws DmException{
        return userLoginService.Userlogin(params);
    }

    /**
     * 注册
     * @return
     */
    @PostMapping(path = "/register")
    public CommonResponse userenroll(@RequestBody Map<String,String> params)throws DmException{
        return ResponseUtil.returnSuccess((userenrollService.userenroll(params)));
    }

    @PostMapping(path = "/p/querypersoninfo")
    public CommonResponse querypersoninfo(HttpServletRequest request){
        // 获取请求消息中的所有头字段
        String  token  = request.getHeader("token");
        return ResponseUtil.returnSuccess(userLoginService.personinfo(Long.parseLong(tokenUtil.token(token))));
    }
}
